<?php
/**
 * 评论管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/12 0012
 * Time: 下午 4:22
 */
namespace app\article\controller\compose;
use app\article\validate\ValiCm;
use app\article\validate\ValidateFun;
use app\common\api\article\IComment;


class Comment implements IComment {
    private $data =[];

    /**
     * 初始化参数
     * Comment constructor.
     * @param array $config
     */
    public function __construct(array $config=[]){
        $this->data = array_filter_full(array_merge($this->data,$config));
    }

    /**
     * 评论列表
     * @return array
     */
    public function cm_list(){
        $default = [
            'curr'=>1,
            'limits'=>10,
            'keyword'=>'',
        ];
        $data = array_filter_full(array_merge($default,$this->data));
        #获取数据
        $list = new \app\article\model\Comment();
        $res = $list->cm_list($data);
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];

    }

    /**
     * 我发表的评论
     */
    public function myCm_list()
    {
        $admin = session('admin_info');
        $default = [
            'from_userid'   =>$admin['id'],
            'curr'          =>1,
            'limits'        =>10,
            'keyword'       =>'',
        ];
        $data = array_filter_full(array_merge($default,$this->data));

        #验证参数
        $vali = ValiCm::myCm_list();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            return ['code'=>$validate->getError(),'msg'=>config('msg.'.$validate->getError())];
        }

        #获取数据
        $list = new \app\article\model\Comment();
        $res = $list->myCm_list($data);
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
    }

    /**
     * 我的文章评论
     */
    public function otherCm_list()
    {
        $admin = session('admin_info');
        $default = [
            'from_userid'   =>$admin['id'],
            'curr'          =>1,
            'limits'        =>10,
            'keyword'       =>'',
        ];
        $data = array_filter_full(array_merge($default,$this->data));

        #验证参数
        $vali = ValiCm::myCm_list();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            return ['code'=>$validate->getError(),'msg'=>config('msg.'.$validate->getError())];
        }

        #获取数据
        $list = new \app\article\model\Comment();
        $res = $list->otherCm_list($data);
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
    }

    /**
     *删除评论
     * @return array
     */
    public function cm_del(){
        $de =[
            'updatetime' =>date('Y-m-d H:i:s'),
            'del'=>0
        ];
        $data = array_filter_full(array_merge($de,$this->data));

        #验证参数
        $vali = ValiCm::cm_del();
        $validate=new ValidateFun($vali['rule'],$vali['message']);
        if(!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>"msg.$err"];
        }
        #删除
        $del = new \app\article\model\Comment();
        $res = $del->cm_del($data);
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }else{
            return ['code'=>400,'msg'=>config('msg.400')];
        }
    }

    /**
     * 发表评论
     * @return array
     */
    public function cm_add(){
        $admin = session('admin_info');
        $de=[
            'from_userid'=>$admin['id'],
            'createtime'=>date('Y-m-d H:i:s'),
            'del'=>1
        ];
        $data = array_filter_full(array_merge($de,$this->data));
        unset($data['id']);

        #验证参数
        $vali = ValiCm::cm_add();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }

        #添加参数
        $add = new \app\article\model\Comment();
        $res = $add ->cm_add($data);
        #返回
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }else{
            return ['code'=>10006,'msg'=>config('msg.10006')];
        }
    }
}